package problem202;

//202.快乐数
//https://leetcode.cn/problems/happy-number/

class Solution {
    public boolean isHappy(int n) {
        int a = n, b = n;
        while(true) {
            //走一步
            a = change(a);
            //走两步
            b = change(change(b));
            // System.out.println(a + " " + b);
            if(a == 1) return true;
            if(a == b) break;
        }
        return false;
    }

    public int change(int n) {
        int sum = 0;
        while(n!=0) {
            int t = n%10;
            sum += t*t;
            n/=10;
        }
        return sum;
    }
}